<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增岗位')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-post-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">商品编号：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="productCode" id="productCode" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">商品名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="name" id="name" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">商品价格：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="price" id="price" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">商品数量：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="count" id="count" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">生产地：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="address" id="address" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">商品备注：</label>
            <div class="col-sm-8">
                <textarea type="text" id="desc" name="desc" style="margin: 0px; width: 478px; height: 137px;"></textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script type="text/javascript">
    var prefix = ctx + "cms/warehouse/goods";

    $("#form-post-add").validate({
        onkeyup: false,
        rules:{
            name:{
                remote: {
                    url: ctx + "cms/warehouse/goods/checkPostNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "name" : function() {
                            return $.common.trim($("#name").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            productCode:{
                remote: {
                    url: ctx + "cms/warehouse/goods/checkPostCodeUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "productCode" : function() {
                            return $.common.trim($("#productCode").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            postSort:{
                digits:true
            },
        },
        messages: {
            "productCode": {
                remote: "商品编码--已经存在"
            },
            "name": {
                remote: "商品名称已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-post-add').serialize());
            // add()
        }
    }

    function add() {
        var productCode = $("input[name='productCode']").val();
        var name = $("input[name='name']").val();
        var price = $("input[name='price']").val();
        var count = $("input[name='count']").val();
        var address = $("input[name='address']").val();
        // 文本域  此处使用的不一样上面全是input
        var desc = $("textarea[name='desc']").val();
        $.ajax({
            cache : true,
            type : "POST",
            url : ctx + "cms/warehouse/goods/add",
            data : {
                "productCode": productCode,
                "name": name,
                "price": price,
                "count": count,
                "address": address,
                "desc": desc,
            },
            async : false,
            error : function(request) {
                $.modal.alertError("系统错误");
            },
            success : function(data) {
                $.operate.successCallback(data);
            }
        });
    }

</script>
</body>
</html>
